package com.isunland.managesystem.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.isunland.managesystem.entity.ExcManContent;
import java.util.List;

public class DelegateTaskDialogFragment extends DialogFragment {
  protected static final String EXTRA_ID = "com.isunland.managesystem.ui.extra_id";
  protected static final String EXTRA_NAME = "com.isunland.managesystem.ui.extra_name";
  private static final String TAG = DelegateTaskDialogFragment.class.getSimpleName();
  private AlertDialog mDialog;
  private ArrayAdapter<ExcManContent> mAdapter;
  private ListView mListView;
  private List<ExcManContent> mexcManList;// 放执行人信息的list

  @Override public void onCreate(Bundle savedInstanceState) {
    // 接收点击搜索传递过来的执行人信息
    mexcManList = (List<ExcManContent>) getArguments().getSerializable("mexcManList");
    super.onCreate(savedInstanceState);
  }

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    mDialog = setAdapter(mexcManList);
    return mDialog;
  }

  private AlertDialog setAdapter(List<ExcManContent> mexcManList2) {
    if (mAdapter == null) {
      mAdapter = new ArrayAdapter<ExcManContent>(getActivity(),
          android.R.layout.select_dialog_singlechoice, mexcManList2) {
        @Override public View getView(int position, View convertView, ViewGroup parent) {
          ExcManContent content = getItem(position);
          if (convertView == null) {
            convertView = LayoutInflater.from(getContext())
                .inflate(android.R.layout.select_dialog_multichoice, parent, false);
          }
          TextView tvName = (TextView) convertView.findViewById(android.R.id.text1);

          tvName.setText(content.getExcManName());
          return convertView;
        }
      };
    } else {
      mAdapter.clear();
      mAdapter.addAll(mexcManList2);
    }
    Builder builderSingle = new AlertDialog.Builder(getActivity());
    builderSingle.setAdapter(mAdapter, null);

    builderSingle.setPositiveButton(android.R.string.ok, new OnClickListener() {

      @Override public void onClick(DialogInterface arg0, int arg1) {
        StringBuilder idBuilder = new StringBuilder();
        StringBuilder nameBuilder = new StringBuilder();
        for (int i = mAdapter.getCount() - 1; i >= 0; i--) {
          if (mListView.isItemChecked(i)) {

            if (idBuilder.length() > 0) {
              idBuilder.append(",").append(mAdapter.getItem(i).getJobno());
            } else {
              idBuilder.append(mAdapter.getItem(i).getJobno());
            }
            if (nameBuilder.length() > 0) {
              nameBuilder.append(",").append(mAdapter.getItem(i).getExcManName());
            } else {
              nameBuilder.append(mAdapter.getItem(i).getExcManName());
            }
          }
        }
        if (getTargetFragment() == null) return;
        Intent i = new Intent();
        i.putExtra(EXTRA_ID, idBuilder.toString());
        i.putExtra(EXTRA_NAME, nameBuilder.toString());

        //Log.i(EXTRA_ITEM, idBuilder.toString());
        //Log.i(EXTRA_NAME, nameBuilder.toString());

        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
      }
    });
    AlertDialog alertDialog = builderSingle.create();
    mListView = alertDialog.getListView();
    mListView.setAdapter(mAdapter);
    mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    mListView.setDivider(null);
    mListView.setDividerHeight(-1);
    alertDialog.setOnShowListener(new OnShowListener() {

      @Override public void onShow(DialogInterface arg0) {

      }
    });
    alertDialog.show();
    return alertDialog;
  }
}
